package m.toolkit.file.model;

import java.io.File;
import java.math.BigDecimal;

import m.Config;
import m.framework.enums.FileModule;
import m.framework.enums.FileType;
import m.framework.exception.ValidateError;
import m.framework.utils.DateTime;
import m.framework.utils.JsonUtil;
import m.framework.utils.LogUtil;
import m.framework.utils.StringUtil;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 
 * @author wanyongming
 * @date 2011-7-24 下午4:30:38
 */

public class FileInfo extends FileInfoDto {
	private static Logger log = LoggerFactory.getLogger(FileInfo.class);
	/** 允许删除的最大文件大小,10M */
	public static long MAX_SIZE = 1024 * 1024 * 10;
	public static long FILE_NAME_LENGTH = 40;
	// 文件上传时出现错误信息,保存在这里
	private String errorMsg;

	public String getMax() {
		return this.pareseSize(MAX_SIZE);
	}

	public String getFileName() {
		if (this.getOriName() == null) {
			return "";
		}
		int index = this.getOriName().lastIndexOf('\\');
		if (index == -1) {
			index = this.getOriName().lastIndexOf('/');
		}
		if (index != -1) {
			return this.getOriName().substring(index + 1);
		}
		return this.getOriName();
	}

	/**
	 * 创建文件并设置fileinfo内容
	 * 
	 * */
	public FileInfo newFile(FileModule module, FileType fileType) {
		DateTime date = DateTime.current();
		StringBuilder timeDir = new StringBuilder();
		timeDir.append(date.toString(DateTime.YEAR_TO_YEAR) + File.separator);
		timeDir.append(date.toString(DateTime.MONTH_TO_MONTH) + File.separator);
		timeDir.append(date.toString(DateTime.DAY_TO_DAY) + File.separator);

		String dir = Config.getUploadRoot() + module + File.separator + timeDir.toString();
		File fd = new File(dir);
		if (!fd.exists()) {
			if (!fd.mkdirs()) {
				log.error("创建目录" + dir + "失败", new Exception());
			}
		}
		this.setPk();
		String realPath = module + File.separator + timeDir + this.pk() + fileType.getExt();
		realPath = realPath.replace('\\', '/');
		this.setReadPath(realPath);
		return this;
	}

	/** 文件实际路径 */
	public String getDownLoadPath() {
		if (StringUtil.isEmpty(this.getReadPath())) {
			return null;
		}
		return Config.getUploadRoot() +"/"+ this.getReadPath();
	}

	public String getShowSize() {
		if (this.getSize() == null) {
			return "";
		}
		return this.pareseSize(this.getSize());
	}

	/** 将文件大小转换为文本显示 */
	private String pareseSize(long size) {
		if (size > 1024 * 1024 * 1024) {
			double no = size / (1024 * 1024 * 1024D);
			BigDecimal b = new BigDecimal(no);
			double result = b.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
			return result + "G";
		}
		if (size > 1024 * 1024) {
			double no = size / (1024 * 1024D);
			BigDecimal b = new BigDecimal(no);
			double result = b.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
			return result + "M";
		}
		double no = size / (1024D);
		BigDecimal b = new BigDecimal(no);
		double result = 0;
		if (no < 0.01) {
			result = b.setScale(3, BigDecimal.ROUND_HALF_UP).doubleValue();
		} else {
			result = b.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
		}
		return result + "K";
	}

	/** 验证文件名是否正确,不正确直接抛 */
	public void validateName() {
		if (StringUtil.isEmpty(this.getOriName())) {
			throw new ValidateError("文件名不能为空");
		}
		if (this.getOriName().matches(StringUtil.REG_INVALID_WORD)) {
			throw new ValidateError("文件名包含非法字符");
		}
		if (this.getOriName().length() > FILE_NAME_LENGTH) {
			throw new ValidateError("文件名长度不能大于" + FILE_NAME_LENGTH);
		}
		for (FileType f : FileType.values()) {
			if (this.getOriName().toLowerCase().endsWith(f.getExt())) {
				return;
			}
		}
		throw new ValidateError("不允许的文件类型");
	}

	public String getErrorMsg() {
		return errorMsg;
	}

	public void setErrorMsg(String errorMsg) {
		this.errorMsg = errorMsg;
	}

}
